package com.example.myone.Pager;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.myone.Base.BasePager;
import com.example.myone.Fragment.LoginFragment;
import com.example.myone.Fragment.MyFragment;
import com.example.myone.R;
import com.example.myone.Util.LoginInfoUtil;

@SuppressLint({"StaticFieldLeak", "NewApi"})
public class MyPager extends BasePager {
    private static FragmentManager fragmentManager;
    private static FrameLayout fragmentContainer;

    public MyPager(Context context, FragmentManager fragmentManager) {
        super(context);
        MyPager.fragmentManager = fragmentManager;
    }

    @Override
    public View initView() {
        Log.d(TAG, "==> MyPager initView");
        View view = LayoutInflater.from(context).inflate(R.layout.my_pager, null);
        fragmentContainer = view.findViewById(R.id.fragment_container);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        Log.d(TAG, "==> MyPager initData");
        if (!LoginInfoUtil.isLogin(context)) {
            replaceFragment(new LoginFragment()); // 待登录界面
        }else {
            replaceFragment(new MyFragment()); // 我的页面
        }
    }

    public static void replaceFragment(Fragment fragment) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(fragmentContainer.getId(), fragment);
        transaction.commitAllowingStateLoss(); // 允许状态丢失
    }
}

